﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using VMDS.Business.Entities;
using VMDS.GUIClient.Helper;
using VMDS.GUIClient.Model;

namespace VMDS.GUIClient.ViewModels.Management.VMs
{
    class VmManagementViewModel:BaseViewModel
    {
        public ObservableCollection<VM> VMs 
        {
            get { return ManagerDataStore.Instance.VMs; }
        }
         
        public VM CurrentVM
        {
            get { return ManagerDataStore.Instance.CurrentVM; }
            set { ManagerDataStore.Instance.CurrentVM = value;
                OnPropertyChanged(()=>DeleteRequest);
                OnPropertyChanged(()=>Change);
            if (CurrentVM != null)
            {
                VmDescr = CurrentVM.Description;
                VmName = CurrentVM.Name;
                IsVisible = CurrentVM.Visible;

            }
            else {VmDescr = "";VmName="";}

                OnPropertyChanged(() => CurrentVM);
                OnPropertyChanged(() => Change);
            }
        }

        private string _vmn;
        public string VmName
        {
            get { return _vmn; }
            set { _vmn = value;
                OnPropertyChanged(() => VmName);
               OnPropertyChanged(()=>Change);
            }
        }

        private string _vmd;
        public string VmDescr
        {
            get { return _vmd; }
            set
            {
                _vmd = value; if (String.IsNullOrEmpty(value)) _vmd = "";
                OnPropertyChanged(() => VmDescr);
                OnPropertyChanged(() => Change);
            }
        }

        private bool visible;
        public bool IsVisible
        {
            get
            {
                return visible;
            }
            set
            {
                visible = value;
                this.OnPropertyChanged(()=>IsVisible);
                this.OnPropertyChanged(()=>Change);
            }
        }

        void SelectScr()
        {
            Screenshots = new ObservableCollection<ScreenShot>();
            if (CurrentVM == null) return;
            foreach (ScreenShot screenShot in
                ManagerDataStore.Instance.ScreenShots.Where(screenShot => screenShot.VMId == CurrentVM.Id))
            {
                Screenshots.Add(screenShot);
            }
        }

        private ObservableCollection<ScreenShot> _scr;
        public ObservableCollection<ScreenShot> Screenshots
        {
            get { return _scr; }
            set
            {
                _scr = value;
                OnPropertyChanged(() => Screenshots);
            }
        }

        private ScreenShot _cscr;
        public ScreenShot CurrentScreenShot
        {
            get { return _cscr; }
            set { _cscr = value;
                OnPropertyChanged(() => CurrentScreenShot);
            }
        }

        private bool _addscr;
        public bool AddScrVisible
        {
            get { return _addscr; }
            set { _addscr = value;
                OnPropertyChanged(() => AddScrVisible);
            }
        }

        public ICommand AddScr { get { return new BaseCommand(() => { AddScrVisible = true; }); } }
        public ICommand Change { get { return getCommand(); } }

        BaseCommand getCommand()
        {
            var c1 = CurrentVM != null;
            var c2 = false;
            if (c1)
            {
                var descr = CurrentVM.Description;
                if (descr==null) descr = "";
                c2 = (descr.ToLower() != VmDescr.ToLower())||(IsVisible!=CurrentVM.Visible);
            }
            return new BaseCommand(change, () => c1&&c2);
        }

        void change()
        {
           
            CurrentVM.Description = VmDescr;
           // MsLocator.Client.UpdateVM(CurrentVM);
            CurrentVM.Visible = IsVisible;
            ServiceLocator.MainService.UpdateVm(CurrentVM);
            ManagerDataStore.Instance.LoadData();
        }
        
        public VmManagementViewModel()
        {
           
            VmName = "";
            VmDescr = "";
            AddScrVisible = false;
            ManagerDataStore.Instance.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Instance_PropertyChanged);
        }
        public ICommand DeleteRequest
        {
            get { return new BaseCommand(Delete, () => CurrentVM != null); }
        }

        void Delete()
        {
            if (MessageBox.Show("Удалить?", "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                try
                {
                    ServiceLocator.MainService.DeleteVM(CurrentVM.Id);
                    VMs.Remove(CurrentVM);
                }
                catch (Exception exception)
                {
                    ShowMessageBox(exception.ToString());
                } 
            }
        }

        void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            OnPropertyChanged(() => VMs);
            OnPropertyChanged(() => CurrentVM);
        }
    }
}
